package com.iwop.moto.controller.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwop.moto.comm.Comm;
import com.iwop.moto.comm.CommUrl;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.model.UpdateToken;
import com.iwop.moto.service.YunxinService;
import com.iwop.moto.utils.api.YunXin.YunXinUtils;
import com.iwop.moto.utils.stringUtil.JsonUtils;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @功能: 网易云信(IM)
 * @日期: liuwb by 2016-10-24 20:29:00 create
 * 云信管理平台地址: http://app.netease.im/login

 */
@Controller
public class YunXinController extends BaseController{

    @Autowired
    private YunxinService yunxinService;
    String getUserInfoUrl="https://api.netease.im/nimserver/user/getUinfos.action";
    String updateUserInfoUrl="+*https://api.netease.im/nimserver/user/updateUinfo.action";
    String value="";

    /**
     * @功能: 云信用户注册测试3
     * @说明: liuwb by 2016-10-24 20:28:25 create
     */
    @RequestMapping(value = "/api/yunxin/test")
    @ResponseBody
    public ResultModel apiYunXinTest(){
        ResultModel rm = new ResultModel();
        try{
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("accid", "Liuweibo"+ SequenceUtil.nextInt(1, 999999)));
            nvps.add(new BasicNameValuePair("name", "波波"+SequenceUtil.nextInt(1, 999999)));
            nvps.add(new BasicNameValuePair("icon", "http://moto.iwop.cn/moto-images/2016/10/21/WX-20161021164858314462080.jpg"));
            String result = YunXinUtils.sendHttpRequest(CommUrl.ADDIMUSER,nvps);
            Object parse = JSON.parse(result);
            rm.setCode(1);
            rm.setData(result);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * @功能: 云信短信模板测试
     */
    @RequestMapping(value = "/api/yunxin/send/sms")
    @ResponseBody
    public ResultModel apiYunXinSendSMS(){
        ResultModel rm = new ResultModel();
        try{
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            String [] mobiles = new String [1];
            mobiles[0] = "15959276686";
            String [] params = new String[2];
            params[0] = "1024";
            params[1] = "5";
            nvps.add(new BasicNameValuePair("templateid", "3029411"));
            nvps.add(new BasicNameValuePair("mobiles", JsonUtils.toJsonString(mobiles)));
            nvps.add(new BasicNameValuePair("params", JsonUtils.toJsonString(params)));
            String result = YunXinUtils.sendHttpRequest(CommUrl.SENDTEMPATE,nvps);
            Object parse = JSON.parse(result);
            rm.setCode(1);
            rm.setData(result);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    //"[\"liuweibo967029\",\"liuweibo801476\"]"
    @RequestMapping(value = "/api/yunxin/user/info")
    @ResponseBody
    public ResultModel apiGetUserInfo(){
        ResultModel rm = new ResultModel();
        try{
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            List list=new ArrayList<>();
            list.add("08000001583534110fac202b6444e58f");
            nvps.add(new BasicNameValuePair("accids", JSON.toJSONString(list)));
            String result = YunXinUtils.sendHttpRequest(getUserInfoUrl,nvps);
            Map parse = (Map)JSON.parse(result);
            rm.setCode(1);
            rm.setData(JSON.toJSONString(parse));
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    //"[\"liuweibo967029\",\"liuweibo801476\"]"
    @RequestMapping(value = "/api/yunxin/update/info")
    @ResponseBody
    public ResultModel apiUpdateUserInfo(){
        ResultModel rm = new ResultModel();
        try{
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("accid","liuweibo801476"));
            nvps.add(new BasicNameValuePair("name","测试"));
            String result = YunXinUtils.sendHttpRequest(updateUserInfoUrl,nvps);
            rm.setCode(1);
            rm.setData(result);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping(value = "/api/yunxin/update/token")
    @ResponseBody
    public ResultModel apiUpdateToken(){
        ResultModel rm = new ResultModel();
        try{
            List<SuperRecordModel> list=yunxinService.getAllIMUser();
            for (SuperRecordModel srm:list){
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("accid",srm.getString("im_accid")));
                String result = YunXinUtils.sendHttpRequest(CommUrl.UPDATETOKEN,nvps);
                UpdateToken updateToken = JSON.parseObject(result, UpdateToken.class);
                UpdateToken.Info info = updateToken.getInfo();
                String token = info.getToken();
                SuperRecordModel superRecordModel=new SuperRecordModel();
                superRecordModel.put("im_token",token);
                superRecordModel.put("im_accid",srm.getString("im_accid"));
                yunxinService.updateIMUser(superRecordModel);
            }
            rm.setCode(1);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

}
